package com.itcast.springboot.controller;

import com.itcast.springboot.dao.ProviderDao;
import com.itcast.springboot.entities.Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * Created by abner-lv on 2019/7/29.
 */
@Controller
public class providerController {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    ProviderDao providerDao;


    //@RequestMapping(value = "/list",method = RequestMethod.GET)
    @GetMapping("/providers")
    public String list(Map<String,Object> map,@RequestParam(value = "providerName",required = false) String providerName){
        logger.info("供应商列表查询。。");

        Collection<Provider> providers = providerDao.getAll(providerName);
        map.put("providers",providers);
        map.put("providerName",providerName);
        return "/provider/list";
    }

    @GetMapping("/provider/{pid}")
    public String view(@PathVariable("pid")Integer pid,
                       @RequestParam(value = "type",defaultValue = "view")String type,
                       Map<String,Object> map){
        logger.info("pid = "+pid);
        Provider provider = providerDao.getProvider(pid);
        map.put("provider",provider);
        return "provider/"+type;
    }

    @PutMapping("/provider")
    public String update(Provider provider){
        logger.info("更新供应商");

        providerDao.save(provider);
        return "redirect:providers";
    }

    @GetMapping("/provider")
   public String toAddPage(){

        return "provider/add";
    }
}
